package com.odriver.neulearn.Activity;


import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.VideoView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.odriver.neulearn.Adapter.CourseLearnBean;
import com.odriver.neulearn.Adapter.LearnListviewAdapter;
import com.odriver.neulearn.Bean.DetailCourseBean;
import com.odriver.neulearn.R;
import com.odriver.neulearn.Util.SaveUtil;
import com.odriver.neulearn.Util.ScreenUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LearnActivity extends AppCompatActivity {

    private String LEARN_URL = "http://www.neuedu.cn/m/mobileCourseInfo!findMoreMcourses.action";
    private ListView listView;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtil.setScreenNoTitle(this);
        setContentView(R.layout.activity_learn);
        initData();
    }

    public void initData() {
        final int code = getIntent().getExtras().getInt("id");
        RequestQueue mQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, LEARN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        CourseLearnBean data = gson.fromJson(response, CourseLearnBean.class);
                        formatData(data.getCourseList());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("网络消息", error.getMessage(), error);
                    }
                }
        ) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("courseid", "" + code);
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("Cookie", SaveUtil.getCookie(LearnActivity.this));
                System.out.println("tmp：" + map.get("Cookie"));
                return map;
            }
        };

        mQueue.add(stringRequest);
    }

    public void formatData(final List<CourseLearnBean.CourseListBean> data) {
        listView = (ListView) findViewById(R.id.idp_learn_list);
        listView.setAdapter(new LearnListviewAdapter(this, data));
        videoView = (VideoView) this.findViewById(R.id.idp_learn_video);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Uri uri = Uri.parse(data.get(i).getMList().get(i).getUrl());
                System.out.println("res:" + data.get(i).getMList().get(i).getUrl());
                videoView.setMediaController(new MediaController(LearnActivity.this));
                videoView.setVideoURI(uri);
                videoView.requestFocus();
                //videoView.start();
            }
        });
    }
}
